package com.ds.bpm.engine.database.admin;

import com.ds.bpm.engine.BPMConstants;
import com.ds.bpm.engine.database.hib.BaseHibernateDAO;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import java.util.List;

public class ProcessDefPersonInstDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(BPMConstants.CONFIG_KEY, ProcessDefPersonInstDAO.class);

    // property constants
    public static final String UUID = "uuid";
    public static final String PROCESSDEFID = "processDefId";
    public static final String PERSONID = "personId";
    public static final String RIGHTCODE = "rightCode";

    public void update(ProcessDefPersonInst transientInstance) {
	log.debug("saving ProcessDefPersonInst instance");
	try {
	    getSession().getTransaction().begin();
	    getSession().update(transientInstance);
	    getSession().getTransaction().commit();
	    log.debug("save successful");
	} catch (RuntimeException re) {
	    log.error("save failed", re);
	    throw re;
	} finally {
	    close();
	}
    }

    public void delete(ProcessDefPersonInst processDefPersonInst) {
	log.debug("deleting ProcessDefPersonInst instance");
	try {
	    getSession().getTransaction().begin();

	    getSession().delete(processDefPersonInst);
	    getSession().getTransaction().commit();
	    log.debug("delete successful");
	} catch (RuntimeException re) {
	    log.error("delete failed", re);
	    throw re;
	} finally {
	    close();
	}
    }

    public ProcessDefPersonInst findById(String id) {
	log.debug("getting ProcessDefPersonInst instance with id: " + id);
	ProcessDefPersonInst instance = null;
	try {
	    instance = (ProcessDefPersonInst) getSession().get("com.ds.bpm.engine.database.admin.ProcessDefPersonInst", id);

	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	} finally {
	    close();
	}
	return instance;
    }

    public List<ProcessDefPersonInst> findByExample(ProcessDefPersonInst instance) {
	log.debug("finding ProcessDefPersonInst instance by example");
	List queryList = null;
	try {
	    queryList = getSession().createCriteria("com.ds.bpm.engine.database.admin.ProcessDefPersonInst").add(Example.create(instance)).list();
	    log.debug("find by example successful, result size: " + queryList.size());

	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	} finally {
	    close();
	}
	return queryList;
    }

    public List<ProcessDefPersonInst> findByProperty(String propertyName, Object value) {
	log.debug("finding ProcessDefPersonInst instance with property: " + propertyName + ", value: " + value);
	List queryList = null;
	try {
	    String queryString = "from ProcessDefPersonInst as model where model." + propertyName + "= ?0";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    queryList = queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	} finally {
	    close();
	}
	return queryList;
    }

    public List<ProcessDefPersonInst> findAll() {
	List queryList = null;
	log.debug("finding all ProcessDefPersonInst instances");
	try {
	    String queryString = "from ProcessDefPersonInst";
	    Query queryObject = getSession().createQuery(queryString);
	    queryList = queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	} finally {
	    close();
	}
	return queryList;
    }

    public void insert(ProcessDefPersonInst instance) {
	log.debug("insert ProcessDefPersonInst instance");
	try {
	    getSession().getTransaction().begin();
	    getSession().insert(instance);
	    getSession().getTransaction().commit();
	    log.debug("insert successful");
	} catch (RuntimeException re) {
	    log.error("insert failed", re);
	    throw re;
	} finally {
	    close();
	}
    }

    public List<ProcessDefPersonInst> findByProcessDefId(String processdefId) {
	return findByProperty(PROCESSDEFID, processdefId);
    }

    public List<ProcessDefPersonInst> findByPersonId(String personId) {
	return findByProperty(PERSONID, personId);
    }

    public List<ProcessDefPersonInst> findByrightCode(String rightCode) {
	return findByProperty(RIGHTCODE, rightCode);
    }

}